package com.run.runbbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.run.runbbs.common.dto.IndexValueDto;
import com.run.runbbs.common.lang.Result;
import com.run.runbbs.entity.*;
import com.run.runbbs.enums.IndexTypeEnum;
import com.run.runbbs.service.*;
import com.run.runbbs.util.IndexValueUtil;
import com.run.runbbs.util.JwtUtils;
import com.run.runbbs.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/indexValue")
public class IndexValueController {
    @Autowired
    private IndexValueService indexValueService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MailUtils mailUtils;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private IndexBloodGlucoseService indexBloodGlucoseService;
    @Autowired
    private IndexPressureService indexPressureService;
    @Autowired
    private IndexSphygmusService indexSphygmusService;
    @Autowired
    private IndexTemperatureService indexTemperatureService;
    @Autowired
    private IndexUricAcidService indexUricAcidService;
    @Autowired
    private IndexWeightService indexWeightService;
    @Autowired
    private IndexCholesterolService indexCholesterolService;


    @GetMapping("/get")
    public Result get(HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        UserInfo userInfo=userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id",userId));
        //取最新的指标值
        //体重
        IndexWeight weight=indexWeightService.getOne(new QueryWrapper<IndexWeight>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //体温
        IndexTemperature temperature=indexTemperatureService.getOne(new QueryWrapper<IndexTemperature>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //血压
        IndexPressure pressure=indexPressureService.getOne(new QueryWrapper<IndexPressure>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //血糖
        IndexBloodglucose bloodGlucose=indexBloodGlucoseService.getOne(new QueryWrapper<IndexBloodglucose>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //脉搏
        IndexSphygmus sphygmus=indexSphygmusService.getOne(new QueryWrapper<IndexSphygmus>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //尿酸
        IndexUricacid uricAcid=indexUricAcidService.getOne(new QueryWrapper<IndexUricacid>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //总胆固醇
        IndexCholesterol cholesterol=indexCholesterolService.getOne(new QueryWrapper<IndexCholesterol>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));

        Map<String,Object> map=new HashMap<>();
        map.put("weight",weight);
        map.put("temperature",temperature);
        map.put("pressure",pressure);
        map.put("bloodGlucose",bloodGlucose);
        map.put("sphygmus",sphygmus);
        map.put("uricAcid",uricAcid);
        map.put("cholesterol",cholesterol);
        return Result.success(map);
    }

    @GetMapping("/getById")
    public Result get(String userId){
        //取最新的指标值
        //体重
        IndexWeight weight=indexWeightService.getOne(new QueryWrapper<IndexWeight>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //体温
        IndexTemperature temperature=indexTemperatureService.getOne(new QueryWrapper<IndexTemperature>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //血压
        IndexPressure pressure=indexPressureService.getOne(new QueryWrapper<IndexPressure>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //血糖
        IndexBloodglucose bloodGlucose=indexBloodGlucoseService.getOne(new QueryWrapper<IndexBloodglucose>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //脉搏
        IndexSphygmus sphygmus=indexSphygmusService.getOne(new QueryWrapper<IndexSphygmus>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //尿酸
        IndexUricacid uricAcid=indexUricAcidService.getOne(new QueryWrapper<IndexUricacid>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));
        //总胆固醇
        IndexCholesterol cholesterol=indexCholesterolService.getOne(new QueryWrapper<IndexCholesterol>().eq("user_id",userId).eq("del_flag",0).orderByDesc("create_time").last("limit 1"));

        Map<String,Object> map=new HashMap<>();
        map.put("weight",weight);
        map.put("temperature",temperature);
        map.put("pressure",pressure);
        map.put("bloodGlucose",bloodGlucose);
        map.put("sphygmus",sphygmus);
        map.put("uricAcid",uricAcid);
        map.put("cholesterol",cholesterol);
        return Result.success(map);
    }

    @PostMapping("/add")
    public Result add(@RequestBody IndexValueDto indexValueDto, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        UserInfo userInfo=userInfoService.getOne(new QueryWrapper<UserInfo>().eq("user_id",userId));
        IndexValue indexValue=new IndexValue();
        indexValue.setUserId(userId);
        indexValue.setIndexType(indexValueDto.getIndexType());
        indexValue.setValue(indexValueDto.getValue());
        Date date=new Date();
        indexValue.setCreateTime(date);
        indexValueService.save(indexValue);


        //异常数据警告
        //先判断是否有绑定关系

        if(indexValueDto.getIndexType().equals("temperature")){
            if(indexValueDto.getValue()>38){
                mailUtils.sendWarnMail("44014815@qq.com","体温",userInfo.getRealname(),date);
            }
        }


        return Result.success("添加成功");
    }

    @GetMapping("/getChartData")
    public Result getChartData(String indexType, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        List<IndexValue> list= indexValueService.getChartData(userId,indexType);
        return Result.success(list);
    }

    @GetMapping("/getPressureChartData")
    public Result getPressureChartData(HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        List<IndexValue> list1= indexValueService.getChartData(userId,"systolic_pressure");
        List<IndexValue> list2= indexValueService.getChartData(userId,"diastolic_pressure");
        List<IndexValue> res=new ArrayList<>();
        for(IndexValue indexValue : list1){
            res.add(indexValue);
        }
        for(IndexValue indexValue : list2){
            res.add(indexValue);
        }
        return Result.success(res);
    }



    @GetMapping("/getIndexValue")
    public Result getIndexValue(String indexType, int offset, HttpServletRequest request){
        String jwt=request.getHeader("Authorization");
        int userId=Integer.parseInt(jwtUtils.getClaimByToken(jwt).getSubject());
        List<IndexValue> list= indexValueService.getIndexValue(userId,indexType,offset,10);
        return Result.success(list);
    }
}
